<template>
  <el-container class="main_container">
    <!--      头部布局-->
    <el-header>
      <!--      logo和项目名称-->
      <div class="left_box">
        <img src="../assets/img/mylogo.png">
      </div>
      <div class="right_box">

        <el-dropdown>
          <img :src="userInfo.avatar">
          <!--          下拉菜单-->
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-house">系统首页</el-dropdown-item>
            <el-dropdown-item icon="el-icon-ship">交流讨论</el-dropdown-item>
            <el-dropdown-item>
              <span type="danger" @click="logout"><span class="el-icon-switch-button"></span> &nbsp;退出登入</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <!--      侧边栏-->
      <el-aside :width="isCollapse?'60px':'200px'">
        <!--        展开/收起-->
        <div class="toggle_box" @click="toggleCollapse">|||</div>
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          background-color="#e0e5e9"
          text-color="black"
          active-text-color="#3F99F6"
          :collapse="isCollapse"
          :default-active="activePath"
          :collapse-transition="false"
          :unique-opened="true"
          :router="true">
          <MenuTree :menuList="this.menuList"></MenuTree>
        </el-menu>
      </el-aside>
      <el-main>
        <!--        路由视图-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import MenuTree from '../components/MenuTree'
import { doLogout, getUserInfo } from '../api/login'
import { mapMutations } from 'vuex'
import { USERLOGOUT } from '../store/types'

export default {
  name: 'Home.vue',
  data () {
    return {
      isCollapse: false,
      activePath:'',
      userInfo: {},
      tableInfo: [],
      menuList: [
        {
          "id": 1,
          "parentId": 0,
          "menuName": "系统管理",
          "url": "",
          "icon": "el-icon-setting",
          "orderNum": 1,
          "open": 0,
          "disabled": false,
          "perms": null,
          "type": 0,
          "children": [
            {
              "id": 253,
              "parentId": 1,
              "menuName": "欢迎页面",
              "url": "/welcome",
              "icon": "el-icon-star-off",
              "orderNum": 1,
              "open": 1,
              "disabled": false,
              "perms": "welcome:view",
              "type": 0,
              "children": []
            },
            {
              "id": 226,
              "parentId": 1,
              "menuName": "员工管理",
              "url": "/users",
              "icon": "el-icon-user",
              "orderNum": 2,
              "open": 0,
              "disabled": false,
              "perms": "users",
              "type": 0,
              "children": []
            },
            {
              "id": 321,
              "parentId": 1,
              "menuName": "客户管理",
              "url": "/customer",
              "icon": "el-icon-picture-outline",
              "orderNum": 2,
              "open": 1,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 4,
              "parentId": 1,
              "menuName": "角色管理",
              "url": "/role",
              "icon": "el-icon-help",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": null,
              "type": 0,
              "children": []
            },
            {
              "id": 235,
              "parentId": 1,
              "menuName": "部门管理",
              "url": "/department",
              "icon": "el-icon-postcard",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 261,
              "parentId": 1,
              "menuName": "菜单管理",
              "url": "/menu",
              "icon": "el-icon-menu",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 262,
              "parentId": 1,
              "menuName": "接口管理",
              "url": "/api",
              "icon": "el-icon-document",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            }
          ]
        },
        {
          "id": 312,
          "parentId": 0,
          "menuName": "业务管理",
          "url": null,
          "icon": "el-icon-s-goods",
          "orderNum": 2,
          "open": 0,
          "disabled": false,
          "perms": null,
          "type": 0,
          "children": [
            {
              "id": 229,
              "parentId": 312,
              "menuName": "合同管理",
              "url": "/contract",
              "icon": "el-icon-date",
              "orderNum": 1,
              "open": 1,
              "disabled": false,
              "perms": "el-icon-date",
              "type": 0,
              "children": []
            },
            // {
            //   "id": 229,
            //   "parentId": 312,
            //   "menuName": "合同审核",
            //   "url": "/conAudit",
            //   "icon": "el-icon-circle-check",
            //   "orderNum": 1,
            //   "open": 1,
            //   "disabled": false,
            //   "perms": "el-icon-date",
            //   "type": 0,
            //   "children": []
            // },
            {
              "id": 310,
              "parentId": 312,
              "menuName": "商品入库",
              "url": "/inStocks",
              "icon": "el-icon-edit",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": null,
              "type": 0,
              "children": []
            },
            // {
            //   "id": 313,
            //   "parentId": 312,
            //   "menuName": "入库审核",
            //   "url": "/inAudit",
            //   "icon": "el-icon-circle-check",
            //   "orderNum": 3,
            //   "open": 0,
            //   "disabled": false,
            //   "perms": null,
            //   "type": 0,
            //   "children": []
            // },
            {
              "id": 311,
              "parentId": 312,
              "menuName": "合同订单",
              "url": "/conOutStocks",
              "icon": "el-icon-date",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": null,
              "type": 0,
              "children": []
            },
            {
              "id": 311,
              "parentId": 312,
              "menuName": "商品发货",
              "url": "/outStocks",
              "icon": "el-icon-edit",
              "orderNum": 3,
              "open": 0,
              "disabled": false,
              "perms": null,
              "type": 0,
              "children": []
            },
            // {
            //   "id": 310,
            //   "parentId": 312,
            //   "menuName": "发货审核",
            //   "url": "/outAudit",
            //   "icon": "el-icon-circle-check",
            //   "orderNum": 3,
            //   "open": 0,
            //   "disabled": false,
            //   "perms": null,
            //   "type": 0,
            //   "children": []
            // },
          ]
        },
        {
          "id": 303,
          "parentId": 0,
          "menuName": "商品管理",
          "url": "",
          "icon": "el-icon-platform-eleme",
          "orderNum": 3,
          "open": 0,
          "disabled": false,
          "perms": "",
          "type": 0,
          "children": [
            {
              "id": 273,
              "parentId": 303,
              "menuName": "商品列表",
              "url": "/goods",
              "icon": "el-icon-s-opportunity",
              "orderNum": 1,
              "open": 1,
              "disabled": false,
              "perms": "map:view",
              "type": 0,
              "children": []
            },
            {
              "id": 304,
              "parentId": 303,
              "menuName": "商品分类",
              "url": "/category",
              "icon": "el-icon-s-cooperation",
              "orderNum": 1,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 305,
              "parentId": 303,
              "menuName": "商品库存",
              "url": "/stocks",
              "icon": "el-icon-c-scale-to-original",
              "orderNum": 2,
              "open": 1,
              "disabled": false,
              "perms": null,
              "type": 0,
              "children": []
            }
          ]
        },
        {
          "id": 295,
          "parentId": 0,
          "menuName": "数据统计",
          "url": "",
          "icon": "el-icon-s-marketing",
          "orderNum": 5,
          "open": 0,
          "disabled": false,
          "perms": "",
          "type": 0,
          "children": [
            {
              "id": 297,
              "parentId": 295,
              "menuName": "个人业绩",
              "url": "/self",
              "icon": "el-icon-user",
              "orderNum": 1,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 341,
              "parentId": 295,
              "menuName": "销售统计",
              "url": "/blog",
              "icon": "el-icon-data-analysis",
              "orderNum": 1,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            },
            {
              "id": 341,
              "parentId": 295,
              "menuName": "商品销售",
              "url": "/staticGood",
              "icon": "el-icon-goods",
              "orderNum": 1,
              "open": 0,
              "disabled": false,
              "perms": "",
              "type": 0,
              "children": []
            }
          ]
        },
        {
          "id": 5,
          "parentId": 0,
          "menuName": "其他管理",
          "url": "/logs",
          "icon": "el-icon-camera",
          "orderNum": 6,
          "open": 0,
          "disabled": false,
          "perms": null,
          "type": 0,
          "children": [
            {
              "id": 271,
              "parentId": 5,
              "menuName": "swagger文档",
              "url": "/swagger",
              "icon": "el-icon-date",
              "orderNum": 1,
              "open": 0,
              "disabled": false,
              "perms": "login:log",
              "type": 0,
              "children": []
            }
          ]
        }
      ]
    }
  },
  components: {
    MenuTree
  },
  methods: {
    ...mapMutations([USERLOGOUT]),
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    async getUser () {
      const res = await getUserInfo()
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('获取用户信息失败:' + res.message)
      } else {
        this.userInfo = res.data.Info
        this.tableInfo.push({
          username: this.userInfo.username,
          phone: this.userInfo.phone,
          deptName: this.userInfo.deptName,
          roleName: this.userInfo.roleName
        });
      }
    },
    async logout() {
      var res = await this.$confirm("此操作将退出系统, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消退出登入"
        });
      });
      if (res == "confirm") {
        const res = await doLogout();
        console.log(res)
        if (res.code == 200){
          this[USERLOGOUT]()
          this.$router.push("/login");
        }
      }
    }
  },
  created () {
    this.getUser()
  },
}
</script>

<style lang="less" scoped>
//设置整个容器高度
.main_container {
  height: 100%;
}

//头部布局
.el-header {
  background-color: #576f50;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  color: #ffffff;
  align-items: center;
  font-size: 20px;
  background: url('../assets/img/login2.png') no-repeat;
  background-size: cover;

  //左边logo和标题
  .left_box {
    display: flex;
    align-items: center;
    //logo
    img {
      width: 180px;
      height: 70px;
      //上右下左
      margin: 0px 0px 5px 15px;
      //border-radius: 50%;
      //background-color: #398d61;
    }

    //标题
    span {
      margin-left: 15px;
    }
  }

  //右边的登录头像
  .right_box {
    .el-dropdown > img {
      height: 60px;
      width: 60px;
      border-radius: 50%;
      background-color: white;
      //上右下左
      margin: 7px 15px 0px 0px;
      background-size: contain;
    }
  }
}

//侧边栏
.el-aside {
  background-color: #e0e5e9;

  .el-menu {
    border-right: none;
  }

  //展开收起
  .toggle_box {
    background-color: #00d9a5;
    font-size: 15px;
    font-weight: bold;
    line-height: 25px;
    color: #ffffff;
    letter-spacing: 0.2em;
    text-align: center;
    cursor: pointer;

  }
}

//内容主体
.el-main {
  background-color: #E9EEF3;
}

body > .el-container {
  margin-bottom: 40px;
}


</style>
